//
//  ModifyPasswordViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/3.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "ResizeView.h"
#import "HttpUserAction.h"

@interface ModifyPasswordViewController ()
@property (nonatomic,strong) UITextField *passwordTF;
@property (nonatomic,strong) UITextField *passwordNewTF;
@property (nonatomic,strong) UITextField *passwordNewAgainTF;

@end

@implementation ModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleName = @"密码修改";
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    ResizeViewInfo *oldPassResizeViewInfo = [self getNormalResizeViewInfo:@"原密码"];
    self.passwordTF = (UITextField*)[oldPassResizeViewInfo viewWithKey:@"TF"];
    _passwordTF.secureTextEntry = YES;
    [resizeView addResizeSubview:oldPassResizeViewInfo];
    ResizeViewInfo *passNewResizeViewInfo = [self getNormalResizeViewInfo:@"新密码"];
    self.passwordNewTF = (UITextField*)[passNewResizeViewInfo viewWithKey:@"TF"];
    _passwordNewTF.secureTextEntry = YES;
    [resizeView addResizeSubview:passNewResizeViewInfo];
    ResizeViewInfo *passwordNewAgainRV = [self getNormalResizeViewInfo:@"重复密码"];
    self.passwordNewAgainTF = (UITextField*)[passwordNewAgainRV viewWithKey:@"TF"];
    _passwordNewAgainTF.secureTextEntry = YES;
    [resizeView addResizeSubview:passwordNewAgainRV];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)placeHolder{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UITextField *textField = [UITextField new];
    textField.font = [UIFont systemFontOfSize:16];
    textField.placeholder = placeHolder;
    [resizeViewInfo addSubview:textField viewKey:@"TF"];
    textField.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 30).widthIs(SCREEN_WIDTH-40).heightIs(30);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(textField, 5).widthIs(SCREEN_WIDTH-40).heightIs(1);
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).heightIs(40).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.passwordTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入密码" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.passwordNewTF.text isEqualToString:@""]) {
        [self showMessage:@"请再次输入密码" autoDissmissAfterDelay:0];
        return;
    }
    if (![self.passwordNewTF.text isEqualToString:self.passwordNewAgainTF.text]) {
        [self showMessage:@"两次输入密码不一致" autoDissmissAfterDelay:0];
        return;
    }
    PasswordActionInfo *passwordActionInfo = [[PasswordActionInfo alloc] init];
    passwordActionInfo.password = self.passwordTF.text;
    passwordActionInfo.againPassword = self.passwordNewTF.text;
    [self showWaitingViewWithMessage:@"修改中..." style:WaitingViewShowStyleFullSelfView];
    [HttpUserAction postModifyPass:passwordActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"修改成功" autoDissmissAfterDelay:0];
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }else{
            [self showMessage:error.domain autoDissmissAfterDelay:0];

        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
